package com.kun.service_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.service_train.entity.Student;
import com.kun.service_train.entity.StudentVo;
import com.kun.service_train.service.StudentService;
import com.kun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-05
 */
@RestController
@RequestMapping("/service_train/student")
@CrossOrigin
public class StudentController {
    @Autowired
    private StudentService studentService;
    @DeleteMapping("delStudent/{id}")
    public R delStudent(@PathVariable String id){
        boolean b = studentService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
    @PostMapping("updateStudent")
    public R updateStudent(@RequestBody Student student){
        boolean b = studentService.updateById(student);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }
    @PostMapping("getStudentById/{id}")
    public R getStudentById(@PathVariable String id){
        Student student = studentService.getById(id);
        return R.ok().data("student",student);
    }
    @PostMapping("addStudent")
    public R addStudent(@RequestBody Student student){
        boolean save = studentService.save(student);
        if (save){
            return R.ok().message("添加成功");
        }else {
            return R.error().message("添加失败");
        }
    }
    @PostMapping("getStudent/{current}/{limit}")

    public R getStudent(@PathVariable Long current, @PathVariable Long limit,@RequestBody(required = false) Student student){

        Page<Student> page = new Page<>(current,limit);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        String stuName = student.getStuName();
        String grade = student.getGrade();
        String classes = student.getClasses();
        if (!ObjectUtils.isEmpty(stuName)){
            wrapper.like("stu_name",stuName);
        }
        if (!ObjectUtils.isEmpty(grade)){
            wrapper.like("grade",grade);
        }
        if (!ObjectUtils.isEmpty(classes)){
            wrapper.like("classes",classes);
        }
        wrapper.orderByDesc("stu_id");
        IPage<Student> page1 = studentService.page(page, wrapper);
        long total = page.getTotal();
        List<Student> records = page.getRecords();
        HashMap<String, Object> map = new HashMap<>();
//        map.put("studentIPage",studentIPage);
        map.put("total",total);
        map.put("records",records);
        return R.ok().data("map",map);
    }
    @PostMapping("addSubjectByFile")
    public R addSubject(MultipartFile file){
        studentService.saveSubject(file,studentService);
        return R.ok();
    }

}

